﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class TestLoader : MonoBehaviour 
{
    private AssetBundleManifest manifest;
    private Stack<AssetBundle> bundles = new Stack<AssetBundle>();

    private void Start()
    {
        var ab = AssetBundle.LoadFromFile(Application.dataPath + "/Bundle/Bundle");
        manifest = ab.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
    }

    [ContextMenu ("加载A")]
    public void LoadA()
    {
        LoadTarget("pa");
    }

    private void LoadTarget(string bundleName)
    {
        var deps = manifest.GetAllDependencies(bundleName);
        Array.Reverse(deps);
        bundles.Clear();
        for (int i = 0; i < deps.Length; i++)
        {
            var curAb = AssetBundle.LoadFromFile(Application.dataPath + "/Bundle/" + deps[i]);
            bundles.Push(curAb);
        }
        var targetAb = AssetBundle.LoadFromFile(Application.dataPath + "/Bundle/" + bundleName);
        bundles.Push(targetAb);
        var ins = Instantiate<GameObject>(targetAb.LoadAsset<GameObject>("PrefabA"));

        Unload(); 
    }

    private void Unload()
    {
        while (bundles.Count > 0)
        {
            var curAb = bundles.Pop();
            curAb.Unload(false);
        }
    }
}
